﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class DBSystemMgr : Singleton<DBSystemMgr>, IBaseData
{
    private bool isResourcesDir;
    private bool waiting;
    private int requestNum;
    private int loadNum;

    public bool CfgInited { get; private set; }
    public JObject ClientCfg { get; private set; }
    private Dictionary<string, string> allString;
    
    public float LoadProgress
    {
        get { return (float)loadNum / requestNum; }
    }
    
    public DBSystemMgr()
    {
        isResourcesDir = Directory.Exists(Application.dataPath + "/Resources");
        allString = new Dictionary<string, string>(); Init();
    }
    
    public void InitAllCfg(System.Object[] allCfg)
    {
        allString.Clear();
        for (int i = 0; i < allCfg.Length; i++)
        {
            var obj = (allCfg[i] as TextAsset);
            allString.Add(obj.name, obj.text);
            EDebug.LogError("obj.name == " + obj.name);
        }
    }

    public void Init()
    {
        string strContent = GetCfgPath("Cfg/ClientCfg.json");
        ClientCfg = JsonConvert.DeserializeObject(strContent) as JObject;
        new TableDataMgr(); CfgInited = true;
    }

    public void LoadCfg(Action action)
    {
        requestNum++; 
        BehaviourUtil.StartCoroutine(RunLoadCfg(action));
    }

    private IEnumerator RunLoadCfg(Action action)
    {
        //先做一帧延时 等待统计发起的加载总数
        yield return 0;
        Label_waitLoad:
        if (waiting)
        {
            yield return 0;
            goto Label_waitLoad;
        }

        if (action != null) { action(); }

        //每帧限制加载数量
        if (++loadNum % 3 == 0)
        {
            waiting = true;
            yield return 0;
            waiting = false;
        }
        if (requestNum == loadNum)
        {
            ZEventCenter.DispatchEvent(new ZEvent(ConstData.CFGLOAD_COMPLETE));
        }
    }

    public string GetCfgPath(string fileName)
    {
        string[] strClip = fileName.Split('/'); string strContent = strClip[1];
        if (Utility.GetPlatForm() == EPlatform.Editor && isResourcesDir)
        {
            strContent = Resources.Load<TextAsset>(fileName).text;
        }
        EDebug.Log("fileName == " + fileName); return strContent;
    }

    public byte[] GetFileContent(string fileName)
    {
        string strContent = GetCfgPath(fileName);
        return Encoding.UTF8.GetBytes(strContent);
    }

    public int GetGlobalInt(int id)
    {
        var dataItem = TableDataMgr.GlobalDataMgr.GetItemById(id);
        if (dataItem != null) { return dataItem.IntParam; }
        return 0;
    }

    public float GetGlobalFloat(int id)
    {
        var dataItem = TableDataMgr.GlobalDataMgr.GetItemById(id);
        if (dataItem != null) { return dataItem.FloatParam; }
        return 0;
    }

    public bool GetGlobalBool(int id)
    {
        var dataItem = TableDataMgr.GlobalDataMgr.GetItemById(id);
        if (dataItem != null) { return dataItem.BooleanParam; }
        return false;
    }
    
    public string GetGlobalString(int id, params object[] args)
    {
        var dataItem = TableDataMgr.GlobalDataMgr.GetItemById(id);
        if (dataItem == null) { return ""; }
        return string.Format(dataItem.StringParam, args);
    }

    public string GetStringByID(int id, params object[] args)
    {
        var table_StrDictionary = TableDataMgr.LanguageMgr.GetItemById(id);
        if (table_StrDictionary == null) { return ""; }
        return string.Format(table_StrDictionary.StrDictionary, args);
    }
}